package com.eda;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.eda.common.ValueConvert;

public class UserInfoActivity extends Activity {

	Button userInfoMytruck;
	Button userInfoMycargo;
	Button userInfoBack;
	Button userInfoSave;
	Button userInfoUpload;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_user_info);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_user_info_title_bar);
		
		EditText userInfoName = (EditText)findViewById(R.id.userInfoName);
		EditText userInfoMobile = (EditText)findViewById(R.id.userInfoMobile);
		EditText userInfoPassword = (EditText)findViewById(R.id.userInfoPassword);
		EditText userInfoConfirm = (EditText)findViewById(R.id.userInfoConfirm);
		EditText userInfoLevel = (EditText)findViewById(R.id.userInfoLevel);
		
		userInfoMytruck = (Button)findViewById(R.id.userInfoMyTruckBut);
		userInfoMycargo = (Button)findViewById(R.id.userInfoMyCargoBut);
		userInfoBack = (Button)findViewById(R.id.userInfoBackBut);
		userInfoSave = (Button)findViewById(R.id.userInfoSaveBut);
		userInfoUpload = (Button)findViewById(R.id.userInfoUploadLocBut);
		
		String trucks = "";
		String cargos = "";
		
		String url = "http://localhost:8080/apiserver/userInfo.do?userId=186";
		HttpClient client = new DefaultHttpClient();
		HttpPost request;
		try {
			request = new HttpPost(new URI(url));
			HttpResponse response = client.execute(request);
			// 判断请求是否成功
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String out = EntityUtils.toString(entity);
					if(out!=null){
						userInfoName.setText(ValueConvert.convert(out).get(0).get(1));
						userInfoMobile.setText(ValueConvert.convert(out).get(0).get(2));
						userInfoPassword.setText(ValueConvert.convert(out).get(0).get(3));
						userInfoConfirm.setText(ValueConvert.convert(out).get(0).get(4));
						userInfoLevel.setText(ValueConvert.convert(out).get(0).get(5));
					}
				}
			}
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		
		
		
		userInfoBack.setOnClickListener(userInfo2Main);
		userInfoSave.setOnClickListener(saveListener);
		userInfoUpload.setOnClickListener(uploadListener);
		userInfoMycargo.setOnClickListener(userInfo2Mycargo);
		userInfoMytruck.setOnClickListener(userInfo2Mytruck);
	}
	
	private OnClickListener userInfo2Main = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			Intent userInfo2MainIntent = new Intent(UserInfoActivity.this,
					MainActivity.class);
			startActivity(userInfo2MainIntent);

		}
	};
	
	//Save user info
	private OnClickListener saveListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			

		}
	};
	
	//upload my location
	private OnClickListener uploadListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {

		}
	};
	
	private OnClickListener userInfo2Mytruck = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			Intent userInfo2MytruckIntent = new Intent(UserInfoActivity.this,
					MyTruckActivity.class);
			startActivity(userInfo2MytruckIntent);

		}
	};
	
	private OnClickListener userInfo2Mycargo = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			Intent userInfo2MytruckIntent = new Intent(UserInfoActivity.this,
					MyTruckActivity.class);
			startActivity(userInfo2MytruckIntent);

		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_info, menu);
		return true;
	}

}
